//
//  LKOpenServiceViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKOpenServiceViewController.h"

@interface LKOpenServiceViewController ()

@property(nonatomic,strong)UFFormView *formView;
@property(nonatomic,assign)BOOL isHOw;
@end

@implementation LKOpenServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"服务开通";
    [self setNavgationRightBtn];
    if (self.startTime.length != 0) {
           [self createFirst];
        self.isHOw = YES;
    }else{
        [self createSecond];
    }

    // Do any additional setup after loading the view.
}
-(void)createFirst{
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
                
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"台签")
                .name(@"")
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .value(@"永久有效");
            }])
            .addRow([UFSwitchRow makeSwitchRow:^(UFSwitchRowMaker * _Nonnull make) {
                make
                .tintColor(yellowDefaultColor)
                .title(@"优惠券")
                .value(SWITCH_ON)
                .name(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, __kindof NSString * _Nonnull value) {
                    if ([value isEqualToString:SWITCH_ON]) {
                        self.isHOw = YES;
                        [self.formView removeFromSuperview];
                        [self createFirst];
                    }else{
                        self.isHOw = NO;
                        [self.formView removeFromSuperview];
                        [self createSecond];
                    }

                });
            }])
            .addRow([UFDatePickerRow makeDatePickerRow:^(UFDatePickerRowMaker * _Nonnull make) {
                make
                .datePickerMode(UIDatePickerModeDate)
                .dateFormat(@"yyyy-MM-dd")
                .title(@"开始时间")
                .name(@"startTime")
                .value([NSString timestampSwitchTime:[self.startTime integerValue] andFormatter:@"YYYY-MM-dd"])
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                });
            }])
            .addRow([UFDatePickerRow makeDatePickerRow:^(UFDatePickerRowMaker * _Nonnull make) {
                make
                .datePickerMode(UIDatePickerModeDate)
                .dateFormat(@"yyyy-MM-dd")
                .title(@"结束时间")
                .name(@"endTime")
                .value([NSString timestampSwitchTime:[self.endTime integerValue] andFormatter:@"YYYY-MM-dd"])
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, NSString * _Nonnull value) {
                });
            }]);

        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(208);
    }];
    self.formView.scrollEnabled = NO;
}
-(void)createSecond{
    self.formView = [UFFormView makeFormView:^(UFFormViewMaker * _Nonnull make) {
        make
        .addSection([UFSection makeSection:^(UFSectionMaker * _Nonnull make) {
            make
            .titleStyle([UFRowTitleStyle makeTitleStyle:^(UFRowTitleStyleMaker * _Nonnull make) {
               make
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor)
                .textAlignment(NSTextAlignmentLeft);
            }])
            .valueStyle([UFTextStyle makeTextStyle:^(UFRowTextStyleMaker * _Nonnull make) {
                make
                .textAlignment(NSTextAlignmentRight)
                .font([UIFont fontWithName:@"PingFangSC-Regular" size:16])
                .color(labelBlackColor);
                
            }])
            .addRow([UFTextFieldRow makeTextFieldRow:^(UFTextFieldRowMaker * _Nonnull make) {
                make
                .editable(NO)
                .height(RowHeightDefault)
                .title(@"台签")
                .name(@"")
                .accessoryType(UFRowAccessoryDisclosureIndicator)
                .value(@"永久有效");
            }])
            .addRow([UFSwitchRow makeSwitchRow:^(UFSwitchRowMaker * _Nonnull make) {
                make
                .tintColor(yellowDefaultColor)
                .title(@"优惠券")
                .value(SWITCH_OFF)
                .name(@"")
                .valueDidChanged(^(__kindof UFRow * _Nonnull row, __kindof NSString * _Nonnull value) {
                    if ([value isEqualToString:SWITCH_ON]) {
                        self.isHOw = YES;
                        [self.formView removeFromSuperview];
                        [self createFirst];
                    }else{
                        self.isHOw = NO;
                        [self.formView removeFromSuperview];
                        [self createSecond];
                    }

                });
            }]);


        }])
        .addToSuperView(self.view);
    }];

    [self.formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).mas_equalTo(NAVIGATION_BAR_HEIGHT + 10);
        make.height.mas_equalTo(208);
    }];
    self.formView.scrollEnabled = NO;
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"保存" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        NSDictionary *param;
        if (!self.isHOw) {
            param = @{@"is_view":@"0",@"mch_id":self.mid,@"agency_business":@"1"};
        }else{
            NSDictionary *dic = [self.formView toDictionary];
            self.startTime = dic[@"startTime"];
            self.endTime = dic[@"endTime"];
            param = @{@"is_view":@"0",@"mch_id":self.mid,@"agency_business":@"1,3",@"valid_coupon":[NSString stringWithFormat:@"%ld",[NSString timeSwitchTimestamp:self.startTime andFormatter:@"YYYY-MM-dd"]],@"valid_two":[NSString stringWithFormat:@"%ld",[NSString timeSwitchTimestamp:self.endTime andFormatter:@"YYYY-MM-dd"]]};
        }
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopYouhuiquan header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"success"]) {

                [weakSelf.navigationController
                 popViewControllerAnimated:YES];
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadShopInformation" object:nil];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
